/*Copyright (c) 2009 Michael Streif
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in
 *all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *THE SOFTWARE.
 */

#include "console.h"
#include "textmode.h"
#include "kmalloc.h"
#include "memcopy.h"
#include "string.h"
#include "keyboard.h"

#define MAX_VIRT_CONSOLES		4


void receiveKeystroke(char key, char modifier);

char* currentDirectory = "root";

int8 currentX = 0;
int8 currentY = 0;

// TODO: handle switching of console colors properly!
int8 consoleColor = BG_BLACK | FG_LIGHTGRAY;
int8 cursorColor = BG_LIGHTGRAY | FG_BLACK;

void setupConsole(uint16 pages)
{
	setupTextmode(pages);

	registerEventHandlerKbd(&receiveKeystroke);
}

void receiveKeystroke(char key, char modifier)
{
	switch (key)
	{
		/*case '\b':
		{

			break;
		}*/

		/*case '\n':
		{
			// process command

			break;
		}*/

		/*case KEY_UP:
		{
			// scroll

			break;
		}

		case KEY_DOWN:
		{
			// scroll

			break;
		}

		case KEY_LEFT:
		{
			// go left

			break;
		}

		case KEY_RIGHT:
		{
			// go right

			break;
		}*/

		case 'd':
		{
			scrollDown();

			break;
		}

		case 'u':
		{
			scrollUp();

			break;
		}

		default:
		{
			putch(key);

			break;
		}
	}
}


void gotoxy(uint8 x, uint8 y)
{
	if ((x < MAX_X) && (y < MAX_Y))
	{
		currentX = x;
		currentY = y;

		updateCursor(currentX, currentY);
	}
}

void clearscreen()
{
	clearScreen(BG_BLACK | FG_LIGHTGRAY);

	gotoxy(0, 0);
}

void puts(char* str)
{
	while (*str != 0)
	{
		putch(*str);
		str++;
	}
}

void putch(char c)
{
	switch (c)
	{
		case '\n':
		{
			if (currentX == MAX_X)
				currentY++;

			currentY++;
			currentX = 0;

			if (currentY >= MAX_Y)
			{
				nextEmptyPage();
				currentY = 0;
			}

			break;
		}

		default:
		{
			if (currentX < MAX_X)
			{
				// we have not reached the end of the line, so print the char
				setCell(c, consoleColor, currentX, currentY);
				currentX++;
			}
			else
			{
				if (currentY + 1 < MAX_Y)
				{
					// we have reached the end of the line and have not reached the end of
					// the console, so put the char into a new line
					currentY++;
					setCell(c, consoleColor, 0, currentY);
					currentX = 1;
				}
				else
				{
					// we have reached the end of the console, so scroll the console
					nextEmptyPage();
					currentX = 0;
					currentY = 0;

					return;
				}
			}

			break;
		}
	}

	updateCursor(currentX, currentY);
}
